﻿$(document).ready(function () {
	$("#txtName").val(getCurrRestaurant().Name);
	$("#txtOpenDays").val(getCurrRestaurant().Open_days);
	$("#txtDescription").val(getCurrRestaurant().Description);
	$("#txtAddress").val(getCurrRestaurant().Address);
	$("#txtTelephone").val(getCurrRestaurant().Telephone);

	$("#cmbCategory").kendoComboBox({
		dataTextField: "Description",
		dataValueField: "ID",
		dataSource: {
			type: "json",
			transport: {
				read: {
					url: "Owner/getCategoryList"
				}
			}
		},
		change: cmbCategory_onChange,
		value: getCurrRestaurant().Category.ID
	}).data("kendoComboBox");

	var $dtpOpenSince = $("#dtpOpenSince").kendoDateTimePicker({
		format: "HH:mm:ss",
		change: dtpOpenSince_onChange,
		value: getCurrRestaurant().Open_time
	}).attr("readonly", "readonly").data("kendoDateTimePicker");

	var $dtpOpenTo = $("#dtpOpenTo").kendoDateTimePicker({
		format: "HH:mm:ss",
		change: dtpOpenTo_onChange,
		value: getCurrRestaurant().Close_time
	}).attr("readonly", "readonly").data("kendoDateTimePicker");

	var $tablesGrid = $("#tablesGrid").kendoGrid({
		datasource: {
			schema: {
				model: {
					id: "ID",
					fields: {
						Description: {
							editable: true,
							validation: {
								required: true
							},
							defaultValue: 'Mesa nueva'
						}
					}
				}
			}
		},
		editable: true,
		pageable: {
			pageSize: 10
		},
		toolbar: [{ name: "create", text: "Agregar Mesa"}],
		columns: [
			{ field: "Description", title: "Descripcion" },
			{ command: [{ name: "destroy", text: "Eliminar" }], title: "&nbsp;", width: 120 }
		]
	}).data("kendoGrid");

	$("span[controlToValidate]").hide();

	$("#btnAddRestaurant").click(function () {
		if (validateForm("#divRestaurantData")) {
			getTables($tablesGrid.dataSource.data());
			saveRestaurant();
		}
	});

	$("#txtName").change(function () {
		getCurrRestaurant().Name = $(this).val();
	});

	$("#txtOpenDays").change(function () {
		getCurrRestaurant().Open_days = $(this).val();
	});

	$("#txtDescription").change(function () {
		getCurrRestaurant().Description = $(this).val();
	});

	$("#txtAddress").change(function () {
		getCurrRestaurant().Address = $(this).val();
	});

	$("#txtTelephone").change(function () {
		getCurrRestaurant().Telephone = $(this).val();
	});

	function cmbCategory_onChange() {
		getCurrRestaurant().Category = this.dataItem(this.select());
	}

	function dtpOpenSince_onChange() {
		getCurrRestaurant().Open_time = kendo.toString(this.value(), 'HH:mm:ss');
	}

	function dtpOpenTo_onChange() {
		getCurrRestaurant().Close_time = kendo.toString(this.value(), 'HH:mm:ss');
	}
});

function getTables(tableGridData) {
	for (var i = 0; i < tableGridData.length; i++) {
		getCurrRestaurant().TableList.push(tableGridData[i]);
	}
}

//function validateRestaurantForm() {
//	var valid = true;

//	$("span[controlToValidate]").each(function () {
//		var control = $("#" + $(this).attr('controlToValidate'));

//		if (valid) {
//			valid = (control.val().length != 0);
//		}

//		$(this).toggle(control.val().length == 0);
//	});

//	return valid;
//}